/**
 * @Package: 	com.group.DAO 
 * @author: 	Liu  
 * @date: 		Dec 28, 2018 10:01:10 PM 
 */
package com.group.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.group.C3P0Utils.C3P0Utils;
import com.group.pojo.GEvaluate;
import com.group.pojo.GWork;

/**
 * @ClassName:	 GEvaluateDAO.java
 * @Description: 任务评价DAO
 * @author:		 Liu
 * @date: 		 Dec 28, 2018 10:01:10 PM 
 */
public class GEvaluateDAO {
	/**
	 * @Function: 	 GEvaluateDAO.java
	 * @Description: 通过小组ID获取任务评价   
	 * @param:		   小组ID         
	 * @return:		  任务评价列表        
	 * @author: 	 Liu
	 * @date:		 Dec 28, 2018 10:04:45 PM 
	 */
	public List<GEvaluate> getEvaListByGroupID(int groupID){
		List <GEvaluate> eva_list = new ArrayList<GEvaluate>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM  g_work NATURAL JOIN g_evaluate WHERE group_id = ?";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, groupID);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				GEvaluate eva = new GEvaluate();
				GWork work = new GWork();
				work.setWorkID(rs.getInt("work_id"));
				work.setWorkTitle(rs.getString("work_title"));
				work.setWorkContent(rs.getString("work_content"));
				work.setWorkBegin(rs.getString("work_begin"));
				work.setWorkEnd(rs.getString("work_end"));
				work.setOpenEva(work.isPassed(rs.getString("work_end")));
				eva.setEvaluateID(rs.getInt("evaluate_id"));
				eva.setEvaluateType(rs.getString("evaluate_type"));
				eva.setEvaluateContent(rs.getString("evaluate_content"));
				eva.setEvaluateDegree(rs.getString("evaluate_degree"));
				eva.setGWork(work);
				eva_list.add(eva);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
		return eva_list;
	}
	
	/**
	 * @Function: 	 GEvaluateDAO.java
	 * @Description: 获取任务分配的小组列表
	 * @param:		   任务ID
	 * @return:		  小组ID列表
	 * @author: 	 Liu
	 * @date:		 Dec 31, 2018 2:16:48 PM 
	 */
	public List<String> getGroupListByWorkID(int workID){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List <String> groupList = new ArrayList<String>();
		String sql = "SELECT * FROM  g_evaluate  WHERE work_id = ?";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, workID);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				groupList.add(rs.getString("group_id"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
		return groupList;
	}
	
	/**
	 * @Function: 	 GEvaluateDAO.java
	 * @Description: 添加评价
	 * @param:		   评价类
	 * @return:		   
	 * @author: 	 Liu
	 * @date:		 Dec 31, 2018 3:09:48 PM 
	 */
	public void addEva(GEvaluate eva) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "INSERT INTO g_evaluate(work_id, group_id)VALUES(?,?)";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, eva.getWorkID());
			pstmt.setInt(2, eva.getGroupID());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
	}
	/**
	 * @Function: 	 GEvaluateDAO.java
	 * @Description: 删除
	 * @param:		   
	 * @return:		 
	 * @author: 	 Liu
	 * @date:		 Dec 31, 2018 7:15:35 PM 
	 */
	public void delEva(int workID) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "DELETE FROM g_evaluate where work_id= ?";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, workID);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
	}
	
	/**
	 * @Function: 	 GEvaluateDAO.java
	 * @Description: 获取任务评价列表
	 * @param:		   任务ID
	 * @return:		  任务评价列表
	 * @author: 	 Liu
	 * @date:		 Jan 2, 2019 8:52:16 AM 
	 */
	public List<GEvaluate> getEvaListByWorkID(int workID){
		List <GEvaluate> eva_list = new ArrayList<GEvaluate>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM  g_group NATURAL JOIN g_evaluate WHERE work_id = ?";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, workID);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				GEvaluate eva = new GEvaluate();
				eva.setEvaluateID(rs.getInt("evaluate_id"));
				eva.setEvaluateType(rs.getString("evaluate_type"));
				eva.setEvaluateContent(rs.getString("evaluate_content"));
				eva.setEvaluateDegree(rs.getString("evaluate_degree"));
				eva.setGroupName(rs.getString("group_Name"));
				eva_list.add(eva);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
		return eva_list;
	}
	
	/**
	 * @Function: 	 GEvaluateDAO.java
	 * @Description: 更新评价
	 * @param:		  评价类
	 * @return:		 
	 * @author: 	 Liu
	 * @date:		 Jan 2, 2019 10:09:08 AM 
	 */
	public void updateEva(GEvaluate eva) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "UPDATE g_evaluate SET evaluate_content = ?, evaluate_degree = ? WHERE evaluate_id = ?";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, eva.getEvaluateContent());
			pstmt.setString(2, eva.getEvaluateDegree());
			pstmt.setInt(3, eva.getEvaluateID());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
	}
}
